
#include <QtCore>
#include <QVector3D>
#include <QVector4D>

#include "entity/cow.h"
#include "object/object.h"

Object* Cow::s_pkCowObject = new Object();

Cow::Cow(Flock& rkFlock) :
    Agent(rkFlock),
    Entity(),
    m_bIsVisible(true)
{
    // Set entity object.
    Q_ASSERT(s_pkCowObject->isValid());
    setObject(*s_pkCowObject);
}

Cow::~Cow()
{
}

Object& Cow::object()
{
    return *s_pkCowObject;
}

Sphere Cow::body() const
{
    Q_ASSERT(s_pkCowObject->isValid());
    return Sphere(position(), (s_pkCowObject->width() + s_pkCowObject->depth() + s_pkCowObject->height()) / 3 / 2);
}

quint32 Cow::id() const
{
    return m_uiCowSoundID;
}

bool Cow::isVisible()
{
    return m_bIsVisible;
}

void Cow::setHeading(qreal angle)
{
    QMatrix4x4 kResult = matrix();
    kResult.setColumn(0, QVector4D(qCos(angle), 0, qSin(angle), 0));
    kResult.setColumn(1, QVector4D(0, 1, 0, 0));
    kResult.setColumn(2, QVector4D(-qSin(angle), 0, qCos(angle), 0));
    setMatrix(kResult);
}

void Cow::setPosition(const QVector3D& rkPosition)
{
    Agent::setPosition(rkPosition);

    QMatrix4x4 kResult = matrix();
    kResult.setColumn(3, QVector4D(rkPosition, 1));
    setMatrix(kResult);
}

void Cow::setPosition(qreal x, qreal y, qreal z)
{
    Agent::setPosition(x, y, z);

    QMatrix4x4 kResult = matrix();
    kResult.setColumn(3, QVector4D(x, y, z, 1));
    setMatrix(kResult);
}

void Cow::setVisible(bool bIsVisible)
{
    m_bIsVisible = bIsVisible;
}

void Cow::setID(quint32 uiID)
{
    m_uiCowSoundID = uiID;
}

#include <QtOpenGL>
void Cow::paint() const
{
    // Check if painting is needed.
    if(!m_bIsVisible)
        return;

    Entity::paint();
}
